package com.jumi.microservice.service.admin.afterseal;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.DelFlag;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.common.utils.DateUtil;
import com.jumi.microservice.common.utils.EnumUtil;
import com.jumi.microservice.constant.reissue.PhoneTypeEnum;
import com.jumi.microservice.constant.reissue.SelfFlagEnum;
import com.jumi.microservice.constant.reissue.SupplierTypeEnum;
import com.jumi.microservice.dto.afterseal.*;
import com.jumi.microservice.dto.reponse.JmUserReponse;
import com.jumi.microservice.entity.AfterSealOrder;
import com.jumi.microservice.entity.AfterSealOrderGoods;
import com.jumi.microservice.enumerate.IsImportEnum;
import com.jumi.microservice.enumerate.LogisticsStatusEnum;
import com.jumi.microservice.enumerate.OrderStatusEnum;
import com.jumi.microservice.mapper.AfterSealOrderGoodsMapper;
import com.jumi.microservice.mapper.AfterSealOrderMapper;
import com.jumi.microservice.material.dto.supplier.JmSupplierResponse;
import com.jumi.microservice.material.dto.warehouse.JmWarehouseResponse;
import com.jumi.microservice.material.service.IJmSupplierService;
import com.jumi.microservice.material.service.IJmWarehouseService;
import com.jumi.microservice.service.IMemberService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangjingdao
 */
@Service
public class AdminAfterSealService {
    @Resource
    AfterSealOrderMapper afterSealOrderMapper;
    @Resource
    AfterSealOrderGoodsMapper afterSealOrderGoodsMapper;
    @DubboReference
    IJmSupplierService supplyClient;
    @DubboReference
    IJmWarehouseService warehouseClient;
    @DubboReference
    IMemberService iMemberService;

    public TableDataInfo<AfterSealAllResponse> getAfterSealAll(AfterSealAllRequest request) {
        TableDataInfo<AfterSealAllResponse> afterSealAllResponseTableDataInfo = new TableDataInfo<>();
        List<AfterSealAllResponse> afterSealAllResponses = new ArrayList<>();
        QueryWrapper<AfterSealOrder> queryWrapper = new QueryWrapper<>();
        if (request.getPhoneType() != null && request.getPhoneType().equals(PhoneTypeEnum.PHONE_TYPE_BUY.getCode())) {
           ResponseResult<Integer> uid = iMemberService.getUidByMobile(request.getPhone());
            if (uid != null) {
                queryWrapper.eq("buyer_id", uid.getData());
            } else {
                return afterSealAllResponseTableDataInfo;
            }
        } else if (request.getPhoneType() != null && request.getPhoneType().equals(PhoneTypeEnum.PHONE_TYPE_RECEIVE.getCode())) {
            queryWrapper.eq("receiver_phone", request.getPhone());
        }
        if (request.getOrderNo() != null && request.getOrderNo() != null) {
            queryWrapper.eq("order_no", request.getOrderNo());
        }
        if (request.getStartTime() != null && request.getEndTime() != null) {
            queryWrapper.between("create_time", request.getStartTime(), request.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
//        IPage<AfterSealOrder> afterSealOrderPage = new Page<>(request.getPageNum(), request.getPageSize());
        List<AfterSealOrder> afterSealOrderPage = afterSealOrderMapper.selectList(queryWrapper);
        List<Integer> userId = afterSealOrderPage.stream().map(AfterSealOrder::getId).collect(Collectors.toList());
        QueryWrapper<AfterSealOrderGoods> afterSealOrderGoodsQueryWrapper = new QueryWrapper<>();
        if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER_TYPE_ONE.getCode())) {
            afterSealOrderGoodsQueryWrapper.eq("supplier_id", request.getWarehouse());
        } else if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER_TYPE_TWO.getCode())) {
            afterSealOrderGoodsQueryWrapper.eq("warehouse", request.getWarehouse());
        }
        if (request.getGoodsName() != null && request.getGoodsName() != null) {
            afterSealOrderGoodsQueryWrapper.like("goods_name", request.getGoodsName());
        }
        afterSealOrderGoodsQueryWrapper.orderByDesc("create_time");
//        IPage<AfterSealOrderGoods> afterSealOrderGoodsPage = new Page<>(request.getPageNum(), request.getPageSize());
        List<AfterSealOrderGoods> afterSealOrderGoodsPage = afterSealOrderGoodsMapper.selectList(afterSealOrderGoodsQueryWrapper);
        List<Integer> goodsId = afterSealOrderGoodsPage.stream().map(AfterSealOrderGoods::getOrderId).collect(Collectors.toList());
        List<Integer> orderId = goodsId.stream().filter(userId::contains).collect(Collectors.toList());
        if (orderId.size() == 0) {
            return afterSealAllResponseTableDataInfo;
        }
        //获取全部供应商
        List<JmSupplierResponse> responses = getSupply();
        //获取全部发货仓
        ResponseResult<List<JmWarehouseResponse>> iJmWarehouseServiceAll = warehouseClient.findAll();
        List<JmWarehouseResponse> jmWarehouseResponses = iJmWarehouseServiceAll.getData();
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", DelFlag.NORMAL);
        queryWrapper.in("id", orderId);
        queryWrapper.orderByDesc("create_time");
        IPage<AfterSealOrder> afterSealOrderIPage = new Page<>(request.getPageNum(), request.getPageSize());
        afterSealOrderIPage = afterSealOrderMapper.selectPage(afterSealOrderIPage, queryWrapper);
        for (AfterSealOrder afterSealOrder : afterSealOrderIPage.getRecords()) {
            AfterSealAllResponse afterSealAllResponse = new AfterSealAllResponse();
            BeanUtils.copyProperties(afterSealOrder, afterSealAllResponse);
            afterSealAllResponse.setId(afterSealOrder.getId());
            afterSealAllResponse.setAfterSealCreateTime(afterSealOrder.getCreateTime().toString());
            AfterSealUserResponse afterSealUserResponse = new AfterSealUserResponse();
            afterSealUserResponse.setUserName(afterSealOrder.getReceiverName());
            afterSealUserResponse.setAddress(afterSealOrder.getReceiverAddress());
            afterSealUserResponse.setPhone(afterSealOrder.getReceiverPhone());
            afterSealAllResponse.setUser(afterSealUserResponse);
            OrderStatusEnum orderStatusEnum = EnumUtil.getByCode(afterSealOrder.getOrderStatus(), OrderStatusEnum.class);
            afterSealAllResponse.setOrderStatusString(orderStatusEnum.getMessage());
            afterSealAllResponse.setAfterSealCreateTime(DateUtil.getTime(afterSealOrder.getCreateTime()));
            afterSealAllResponse.setCancelRemark(afterSealOrder.getCancelRemark()==null?"":afterSealOrder.getCancelRemark());
            afterSealAllResponses.add(afterSealAllResponse);
        }
        for (AfterSealAllResponse afterSealAllResponse : afterSealAllResponses) {
            List<AfterSealGoodsResponse> afterSealGoodsResponses = new ArrayList<>();
            for (AfterSealOrderGoods record : afterSealOrderGoodsPage) {
                if (afterSealAllResponse.getId() == record.getOrderId()) {
                    AfterSealGoodsResponse afterSealGoodsResponse = new AfterSealGoodsResponse();
                    BeanUtils.copyProperties(record, afterSealGoodsResponse);
                    if (record.getSelfFlag().equals(SelfFlagEnum.SELF_FLAG_NO.getCode().byteValue())) {
                        for (JmSupplierResponse response : responses) {
                            if (record.getSupplierId().equals(response.getSupplierId().intValue())) {
                                afterSealGoodsResponse.setWarehouse(response.getSupplierCompanyName());
                                afterSealGoodsResponse.setWarehouseId(record.getSupplierId());
                            }
                        }
                    } else if (record.getSelfFlag().equals(SelfFlagEnum.SELF_FLAG_YES.getCode().byteValue())) {
                        for (JmWarehouseResponse jmWarehouseRespons : jmWarehouseResponses) {
                            if (record.getWarehouse().equals(jmWarehouseRespons.getWarehouseId().intValue())) {
                                afterSealGoodsResponse.setWarehouse(jmWarehouseRespons.getWarehouseName());
                                afterSealGoodsResponse.setWarehouseId(record.getWarehouse());
                            }
                        }
                    }
                    afterSealGoodsResponses.add(afterSealGoodsResponse);
                }
            }
            afterSealAllResponse.setGoodsList(afterSealGoodsResponses);
        }
        afterSealAllResponseTableDataInfo.setTotal(afterSealOrderIPage.getTotal());
        afterSealAllResponseTableDataInfo.setRows(afterSealAllResponses);
        return afterSealAllResponseTableDataInfo;
    }

    public TableDataInfo<AfterSealAllResponse> getAfterSealSelf(AfterSealSelfRequest request) {
        TableDataInfo<AfterSealAllResponse> afterSealAllResponseTableDataInfo = new TableDataInfo<>();
        List<AfterSealAllResponse> afterSealAllResponses = new ArrayList<>();
        QueryWrapper<AfterSealOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status", OrderStatusEnum.READY_TO_SHIP.getCode());
        if (request.getIds() != null) {
            String[] ids = request.getIds().split(",");
            List<Integer> orderIds = new ArrayList<>();
            for (String id : ids) {
                orderIds.add(Integer.valueOf(id));
            }
            queryWrapper.and(afterSealOrderQueryWrapper -> afterSealOrderQueryWrapper.in("id", orderIds));
        }
        List<AfterSealOrder> afterSealOrderPage = afterSealOrderMapper.selectList(queryWrapper);
        List<Integer> userId = afterSealOrderPage.stream().map(AfterSealOrder::getId).collect(Collectors.toList());
        QueryWrapper<AfterSealOrderGoods> afterSealOrderGoodsQueryWrapper = new QueryWrapper<>();
        afterSealOrderGoodsQueryWrapper.lambda()
                .eq(AfterSealOrderGoods::getSelfFlag, SelfFlagEnum.SELF_FLAG_YES.getCode()).eq(AfterSealOrderGoods::getGoodsStatus, LogisticsStatusEnum.WAIT_SEND.getCode());
        if (request.getWarehouse() != null) {
            afterSealOrderGoodsQueryWrapper.eq("warehouse", request.getWarehouse());
        }
        afterSealOrderGoodsQueryWrapper.orderByDesc("create_time");
        List<AfterSealOrderGoods> afterSealOrderGoodsPage = afterSealOrderGoodsMapper.selectList(afterSealOrderGoodsQueryWrapper);
        List<Integer> goodsId = afterSealOrderGoodsPage.stream().map(AfterSealOrderGoods::getOrderId).collect(Collectors.toList());
        List<Integer> orderId = goodsId.stream().filter(userId::contains).collect(Collectors.toList());
        if (orderId.size() == 0) {
            return afterSealAllResponseTableDataInfo;
        }
        //获取全部发货仓
        ResponseResult<List<JmWarehouseResponse>> iJmWarehouseServiceAll = warehouseClient.findAll();
        List<JmWarehouseResponse> jmWarehouseResponses = iJmWarehouseServiceAll.getData();
        queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", orderId);
        queryWrapper.orderByDesc("create_time");
        IPage<AfterSealOrder> iPage = new Page<>(request.getPageNum(), request.getPageSize());
        iPage = afterSealOrderMapper.selectPage(iPage, queryWrapper);
        for (AfterSealOrder afterSealOrder : iPage.getRecords()) {
            AfterSealAllResponse afterSealAllResponse = new AfterSealAllResponse();
            BeanUtils.copyProperties(afterSealOrder, afterSealAllResponse);
            afterSealAllResponse.setAfterSealCreateTime(afterSealOrder.getCreateTime().toString());
            AfterSealUserResponse afterSealUserResponse = new AfterSealUserResponse();
            afterSealUserResponse.setUserName(afterSealOrder.getReceiverName());
            afterSealUserResponse.setAddress(afterSealOrder.getReceiverAddress());
            afterSealUserResponse.setPhone(afterSealOrder.getReceiverPhone());
            afterSealAllResponse.setUser(afterSealUserResponse);
            OrderStatusEnum orderStatusEnum = EnumUtil.getByCode(afterSealOrder.getOrderStatus(), OrderStatusEnum.class);
            afterSealAllResponse.setOrderStatusString(orderStatusEnum.getMessage());
            afterSealAllResponse.setAfterSealCreateTime(DateUtil.getTime(afterSealOrder.getCreateTime()));
            afterSealAllResponses.add(afterSealAllResponse);
        }
        for (AfterSealAllResponse afterSealAllResponse : afterSealAllResponses) {
            List<AfterSealGoodsResponse> afterSealGoodsResponses = new ArrayList<>();
            for (AfterSealOrderGoods record : afterSealOrderGoodsPage) {
                if (afterSealAllResponse.getId() == record.getOrderId()) {
                    AfterSealGoodsResponse afterSealGoodsResponse = new AfterSealGoodsResponse();
                    BeanUtils.copyProperties(record, afterSealGoodsResponse);
//                    if (record.getSelfFlag().equals(SelfFlagEnum.SELF_FLAG_YES.getCode())) {
                    for (JmWarehouseResponse jmWarehouseResponse : jmWarehouseResponses) {
                        if (record.getWarehouse().equals(jmWarehouseResponse.getWarehouseId().intValue())) {
                            afterSealGoodsResponse.setWarehouse(jmWarehouseResponse.getWarehouseName());
                            afterSealGoodsResponse.setWarehouseId(record.getWarehouse());
                        }
                    }
//                    }
                    afterSealGoodsResponses.add(afterSealGoodsResponse);
                }
            }
            afterSealAllResponse.setGoodsList(afterSealGoodsResponses);
        }
        afterSealAllResponseTableDataInfo.setRows(afterSealAllResponses);
        afterSealAllResponseTableDataInfo.setTotal(iPage.getTotal());
        return afterSealAllResponseTableDataInfo;
    }

    public TableDataInfo<AfterSealAllResponse> getAfterSealNoSelf(AfterSealNoSelfRequest request) {
        TableDataInfo<AfterSealAllResponse> afterSealAllResponseTableDataInfo = new TableDataInfo<>();
        List<AfterSealAllResponse> afterSealAllResponses = new ArrayList<>();
        QueryWrapper<AfterSealOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status", OrderStatusEnum.READY_TO_SHIP.getCode());
        if (request.getStartTime() != null && request.getEndTime() != null) {
            queryWrapper.between("create_time", request.getStartTime(), request.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
//        IPage<AfterSealOrder> afterSealOrderPage = new Page<>(request.getPageNum(), request.getPageSize());
        List<AfterSealOrder> afterSealOrderPage = afterSealOrderMapper.selectList(queryWrapper);
        List<Integer> userId = afterSealOrderPage.stream().map(AfterSealOrder::getId).collect(Collectors.toList());
        QueryWrapper<AfterSealOrderGoods> afterSealOrderGoodsQueryWrapper = new QueryWrapper<>();
        afterSealOrderGoodsQueryWrapper.eq("self_flag", SelfFlagEnum.SELF_FLAG_NO.getCode());
        if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER_TYPE_ONE.getCode())) {
            afterSealOrderGoodsQueryWrapper.eq("supplier_id", request.getWarehouse());
        } else if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER_TYPE_TWO.getCode())) {
            afterSealOrderGoodsQueryWrapper.eq("warehouse", request.getWarehouse());
        }
        if (request.getGoodsName() != null && request.getGoodsName() != null) {
            afterSealOrderGoodsQueryWrapper.like("goods_name", request.getGoodsName());
        }
//        IPage<AfterSealOrderGoods> afterSealOrderGoodsPage = new Page<>(request.getPageNum(), request.getPageSize());
        afterSealOrderGoodsQueryWrapper.orderByDesc("create_time");
        List<AfterSealOrderGoods> afterSealOrderGoodsPage = afterSealOrderGoodsMapper.selectList(afterSealOrderGoodsQueryWrapper);
        List<Integer> goodsId = afterSealOrderGoodsPage.stream().map(AfterSealOrderGoods::getOrderId).collect(Collectors.toList());
        List<Integer> orderId = goodsId.stream().filter(userId::contains).collect(Collectors.toList());
        if (orderId.size() == 0) {
            return afterSealAllResponseTableDataInfo;
        }
        //获取全部供应商
        List<JmSupplierResponse> responses = getSupply();
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", DelFlag.NORMAL);
        queryWrapper.in("id", orderId);
        queryWrapper.orderByDesc("create_time");
        IPage<AfterSealOrder> afterSealOrderIPage = new Page<>(request.getPageNum(), request.getPageSize());
        afterSealOrderIPage = afterSealOrderMapper.selectPage(afterSealOrderIPage,queryWrapper);
        for (AfterSealOrder afterSealOrder : afterSealOrderIPage.getRecords()) {
            AfterSealAllResponse afterSealAllResponse = new AfterSealAllResponse();
            BeanUtils.copyProperties(afterSealOrder, afterSealAllResponse);
            afterSealAllResponse.setAfterSealCreateTime(afterSealOrder.getCreateTime().toString());
            AfterSealUserResponse afterSealUserResponse = new AfterSealUserResponse();
            afterSealUserResponse.setUserName(afterSealOrder.getReceiverName());
            afterSealUserResponse.setAddress(afterSealOrder.getReceiverAddress());
            afterSealUserResponse.setPhone(afterSealOrder.getReceiverPhone());
            afterSealAllResponse.setUser(afterSealUserResponse);
            OrderStatusEnum orderStatusEnum = EnumUtil.getByCode(afterSealOrder.getOrderStatus(), OrderStatusEnum.class);
            afterSealAllResponse.setOrderStatusString(orderStatusEnum.getMessage());
            afterSealAllResponse.setAfterSealCreateTime(DateUtil.getTime(afterSealOrder.getCreateTime()));
            afterSealAllResponses.add(afterSealAllResponse);
        }
        for (AfterSealAllResponse afterSealAllResponse : afterSealAllResponses) {
            List<AfterSealGoodsResponse> sealGoodsResponses = new ArrayList<>();
            for (AfterSealOrderGoods record : afterSealOrderGoodsPage) {
                if (afterSealAllResponse.getId() == record.getOrderId()) {
                    AfterSealGoodsResponse afterSealGoodsResponse = new AfterSealGoodsResponse();
                    BeanUtils.copyProperties(record, afterSealGoodsResponse);
                    for (JmSupplierResponse response : responses) {
                        if (record.getSupplierId().equals(response.getSupplierId().intValue())) {
                            afterSealGoodsResponse.setWarehouse(response.getSupplierCompanyName());
                            afterSealGoodsResponse.setWarehouseId(record.getSupplierId());
                        }
                    }
                    sealGoodsResponses.add(afterSealGoodsResponse);
                }
            }
            afterSealAllResponse.setGoodsList(sealGoodsResponses);
        }
        afterSealAllResponseTableDataInfo.setRows(afterSealAllResponses);
        afterSealAllResponseTableDataInfo.setTotal(afterSealOrderIPage.getTotal());
        return afterSealAllResponseTableDataInfo;
    }

    public TableDataInfo<AfterSealAllResponse> getAfterSealGoodsReceived(AfterSealGoodsReceivedRequest request) {
        TableDataInfo<AfterSealAllResponse> afterSealAllResponseTableDataInfo = new TableDataInfo<>();
        List<AfterSealAllResponse> afterSealAllResponses = new ArrayList<>();
        QueryWrapper<AfterSealOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status", OrderStatusEnum.READY_TO_RECEIVE.getCode());
        if (request.getStartTime() != null && request.getEndTime() != null) {
            queryWrapper.between("create_time", request.getStartTime(), request.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
//        IPage<AfterSealOrder> afterSealOrderPage = new Page<>(request.getPageNum(), request.getPageSize());
        List<AfterSealOrder> afterSealOrderPage = afterSealOrderMapper.selectList(queryWrapper);
        List<Integer> userId = afterSealOrderPage.stream().map(AfterSealOrder::getId).collect(Collectors.toList());
        QueryWrapper<AfterSealOrderGoods> afterSealOrderGoodsQueryWrapper = new QueryWrapper<>();
        if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER_TYPE_ONE.getCode())) {
            afterSealOrderGoodsQueryWrapper.eq("supplier_id", request.getWarehouse());
        } else if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER_TYPE_TWO.getCode())) {
            afterSealOrderGoodsQueryWrapper.eq("warehouse", request.getWarehouse());
        }
        if (request.getGoodsName() != null && request.getGoodsName() != null) {
            afterSealOrderGoodsQueryWrapper.like("goods_name", request.getGoodsName());
        }
//        IPage<AfterSealOrderGoods> afterSealOrderGoodsPage = new Page<>(request.getPageNum(), request.getPageSize());
        afterSealOrderGoodsQueryWrapper.orderByDesc("create_time");
        List<AfterSealOrderGoods> afterSealOrderGoodsPage = afterSealOrderGoodsMapper.selectList(afterSealOrderGoodsQueryWrapper);
        List<Integer> goodsId = afterSealOrderGoodsPage.stream().map(AfterSealOrderGoods::getOrderId).collect(Collectors.toList());
        List<Integer> orderId = goodsId.stream().filter(userId::contains).collect(Collectors.toList());
        if (orderId.size() == 0) {
            return afterSealAllResponseTableDataInfo;
        }
        //获取全部供应商
        List<JmSupplierResponse> responses = getSupply();
        //获取全部发货仓
        ResponseResult<List<JmWarehouseResponse>> iJmWarehouseServiceAll = warehouseClient.findAll();
        List<JmWarehouseResponse> jmWarehouseResponses = iJmWarehouseServiceAll.getData();
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", DelFlag.NORMAL);
        queryWrapper.in("id", orderId);
        queryWrapper.orderByDesc("create_time");
        IPage<AfterSealOrder> afterSealOrderIPage = new Page<>(request.getPageNum(),request.getPageSize());
        afterSealOrderIPage = afterSealOrderMapper.selectPage(afterSealOrderIPage,queryWrapper);
        for (AfterSealOrder afterSealOrder : afterSealOrderIPage.getRecords()) {
            AfterSealAllResponse afterSealAllResponse = new AfterSealAllResponse();
            BeanUtils.copyProperties(afterSealOrder, afterSealAllResponse);
            afterSealAllResponse.setAfterSealCreateTime(afterSealOrder.getCreateTime().toString());
            AfterSealUserResponse afterSealUserResponse = new AfterSealUserResponse();
            afterSealUserResponse.setUserName(afterSealOrder.getReceiverName());
            afterSealUserResponse.setAddress(afterSealOrder.getReceiverAddress());
            afterSealUserResponse.setPhone(afterSealOrder.getReceiverPhone());
            afterSealAllResponse.setUser(afterSealUserResponse);
            OrderStatusEnum orderStatusEnum = EnumUtil.getByCode(afterSealOrder.getOrderStatus(), OrderStatusEnum.class);
            afterSealAllResponse.setOrderStatusString(orderStatusEnum.getMessage());
            afterSealAllResponse.setAfterSealCreateTime(DateUtil.getTime(afterSealOrder.getCreateTime()));
            afterSealAllResponses.add(afterSealAllResponse);
        }
        for (AfterSealAllResponse afterSealAllResponse : afterSealAllResponses) {
            List<AfterSealGoodsResponse> afterSealGoodsResponses = new ArrayList<>();
            for (AfterSealOrderGoods record : afterSealOrderGoodsPage) {
                AfterSealGoodsResponse afterSealGoodsResponse = new AfterSealGoodsResponse();
                if (afterSealAllResponse.getId() == record.getOrderId()) {
                    BeanUtils.copyProperties(record, afterSealGoodsResponse);
                    if (record.getSelfFlag().equals(SelfFlagEnum.SELF_FLAG_NO.getCode().byteValue())) {
                        for (JmSupplierResponse response : responses) {
                            if (record.getSupplierId().equals(response.getSupplierId().intValue())) {
                                afterSealGoodsResponse.setWarehouse(response.getSupplierCompanyName());
                                afterSealGoodsResponse.setWarehouseId(record.getSupplierId());
                            }
                        }
                    } else if (record.getSelfFlag().equals(SelfFlagEnum.SELF_FLAG_YES.getCode().byteValue())) {
                        for (JmWarehouseResponse jmWarehouseRespons : jmWarehouseResponses) {
                            if (record.getWarehouse().equals(jmWarehouseRespons.getWarehouseId().intValue())) {
                                afterSealGoodsResponse.setWarehouse(jmWarehouseRespons.getWarehouseName());
                                afterSealGoodsResponse.setWarehouseId(record.getWarehouse());
                            }
                        }
                    }
                    afterSealGoodsResponses.add(afterSealGoodsResponse);
                }
            }
            afterSealAllResponse.setGoodsList(afterSealGoodsResponses);
        }
        afterSealAllResponseTableDataInfo.setTotal(afterSealOrderIPage.getTotal());
        afterSealAllResponseTableDataInfo.setRows(afterSealAllResponses);
        return afterSealAllResponseTableDataInfo;

    }

    public TableDataInfo<AfterSealAllResponse> getAfterSealCompleted(AfterSealGoodsReceivedRequest request) {
        TableDataInfo<AfterSealAllResponse> afterSealAllResponseTableDataInfo = new TableDataInfo<>();
        List<AfterSealAllResponse> afterSealAllResponses = new ArrayList<>();
        QueryWrapper<AfterSealOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status", OrderStatusEnum.COMPLETED.getCode());
        if (request.getStartTime() != null && request.getEndTime() != null) {
            queryWrapper.between("create_time", request.getStartTime(), request.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
//        IPage<AfterSealOrder> afterSealOrderPage = new Page<>(request.getPageNum(), request.getPageSize());
        List<AfterSealOrder> afterSealOrderPage = afterSealOrderMapper.selectList(queryWrapper);
        List<Integer> userId = afterSealOrderPage.stream().map(AfterSealOrder::getId).collect(Collectors.toList());
        QueryWrapper<AfterSealOrderGoods> afterSealOrderGoodsQueryWrapper = new QueryWrapper<>();
        if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER_TYPE_ONE.getCode())) {
            afterSealOrderGoodsQueryWrapper.eq("supplier_id", request.getWarehouse());
        } else if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER_TYPE_TWO.getCode())) {
            afterSealOrderGoodsQueryWrapper.eq("warehouse", request.getWarehouse());
        }
        if (request.getGoodsName() != null && request.getGoodsName() != null) {
            afterSealOrderGoodsQueryWrapper.like("goods_name", request.getGoodsName());
        }
        afterSealOrderGoodsQueryWrapper.orderByDesc("create_time");
//        IPage<AfterSealOrderGoods> afterSealOrderGoodsPage = new Page<>(request.getPageNum(), request.getPageSize());
        List<AfterSealOrderGoods> afterSealOrderGoodsPage = afterSealOrderGoodsMapper.selectList(afterSealOrderGoodsQueryWrapper);
        List<Integer> goodsId = afterSealOrderGoodsPage.stream().map(AfterSealOrderGoods::getOrderId).collect(Collectors.toList());
        List<Integer> orderId = goodsId.stream().filter(userId::contains).collect(Collectors.toList());
        if (orderId.size() == 0) {
            return afterSealAllResponseTableDataInfo;
        }
        //获取全部供应商
        List<JmSupplierResponse> responses = getSupply();
        //获取全部发货仓
        ResponseResult<List<JmWarehouseResponse>> iJmWarehouseServiceAll = warehouseClient.findAll();
        List<JmWarehouseResponse> jmWarehouseResponses = iJmWarehouseServiceAll.getData();
        queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", orderId);
        queryWrapper.orderByDesc("create_time");
        IPage<AfterSealOrder> afterSealOrderIPage = new Page<>(request.getPageNum(), request.getPageSize());
        afterSealOrderIPage = afterSealOrderMapper.selectPage(afterSealOrderIPage,queryWrapper);
        for (AfterSealOrder afterSealOrder : afterSealOrderIPage.getRecords()) {
            AfterSealAllResponse afterSealAllResponse = new AfterSealAllResponse();
            BeanUtils.copyProperties(afterSealOrder, afterSealAllResponse);
            afterSealAllResponse.setAfterSealCreateTime(afterSealOrder.getCreateTime().toString());
            AfterSealUserResponse afterSealUserResponse = new AfterSealUserResponse();
            afterSealUserResponse.setUserName(afterSealOrder.getReceiverName());
            afterSealUserResponse.setAddress(afterSealOrder.getReceiverAddress());
            afterSealUserResponse.setPhone(afterSealOrder.getReceiverPhone());
            afterSealAllResponse.setUser(afterSealUserResponse);
            OrderStatusEnum orderStatusEnum = EnumUtil.getByCode(afterSealOrder.getOrderStatus(), OrderStatusEnum.class);
            afterSealAllResponse.setOrderStatusString(orderStatusEnum.getMessage());
            afterSealAllResponse.setAfterSealCreateTime(DateUtil.getTime(afterSealOrder.getCreateTime()));
            afterSealAllResponses.add(afterSealAllResponse);
        }
        for (AfterSealAllResponse afterSealAllResponse : afterSealAllResponses) {
            List<AfterSealGoodsResponse> afterSealGoodsResponses = new ArrayList<>();
            for (AfterSealOrderGoods record : afterSealOrderGoodsPage) {
                if (afterSealAllResponse.getId() == record.getOrderId()) {
                    AfterSealGoodsResponse afterSealGoodsResponse = new AfterSealGoodsResponse();
                    BeanUtils.copyProperties(record, afterSealGoodsResponse);
                    if (record.getSelfFlag().equals(com.jumi.microservice.enumerate.SelfFlagEnum.NO_SELF.getCode())) {
                        for (JmSupplierResponse response : responses) {
                            if (record.getSupplierId().equals(response.getSupplierId().intValue())) {
                                afterSealGoodsResponse.setWarehouse(response.getSupplierCompanyName());
                                afterSealGoodsResponse.setWarehouseId(record.getSupplierId());
                            }
                        }
                    } else if (record.getSelfFlag().equals(com.jumi.microservice.enumerate.SelfFlagEnum.YES_SELF.getCode())) {
                        for (JmWarehouseResponse jmWarehouseRespons : jmWarehouseResponses) {
                            if (record.getWarehouse().equals(jmWarehouseRespons.getWarehouseId().intValue())) {
                                afterSealGoodsResponse.setWarehouse(jmWarehouseRespons.getWarehouseName());
                                afterSealGoodsResponse.setWarehouseId(record.getWarehouse());
                            }
                        }
                    }
                    afterSealGoodsResponses.add(afterSealGoodsResponse);
                }
            }
            afterSealAllResponse.setGoodsList(afterSealGoodsResponses);
        }
        afterSealAllResponseTableDataInfo.setTotal(afterSealOrderIPage.getTotal());
        afterSealAllResponseTableDataInfo.setRows(afterSealAllResponses);
        return afterSealAllResponseTableDataInfo;

    }

    public ResponseResult<AfterSealDetailsResponse> getAfterOrderDetails(AfterOrderDetailsRequest request) {
        ResponseResult<AfterSealDetailsResponse> responseResponseResult = new ResponseResult<>();
        AfterSealDetailsResponse afterSealDetailsResponse = new AfterSealDetailsResponse();
        QueryWrapper<AfterSealOrder> afterSealOrderQueryWrapper = new QueryWrapper<>();
        afterSealOrderQueryWrapper.eq("id", request.getId());
        AfterSealOrder afterSealOrder = afterSealOrderMapper.selectOne(afterSealOrderQueryWrapper);
        BeanUtils.copyProperties(afterSealOrder, afterSealDetailsResponse);
        afterSealDetailsResponse.setOrderStatusString(EnumUtil.getByCode(afterSealOrder.getOrderStatus(), OrderStatusEnum.class).getMessage());
        ResponseResult<JmUserReponse> jmUserReponseR = iMemberService.getMemberByUid(afterSealOrder.getBuyerId().intValue());
        afterSealDetailsResponse.setNickName(jmUserReponseR.getData().getName());
        afterSealDetailsResponse.setLevel(jmUserReponseR.getData().getLevel().getDesc());
        afterSealDetailsResponse.setPhone(jmUserReponseR.getData().getMobile());
        QueryWrapper<AfterSealOrderGoods> afterSealOrderGoodsQueryWrapper = new QueryWrapper<>();
        afterSealOrderGoodsQueryWrapper.eq("order_id", request.getId());
        AfterSealOrderGoods afterSealOrderGoods = afterSealOrderGoodsMapper.selectOne(afterSealOrderGoodsQueryWrapper);
        List<AfterSealOrderDetailsResponse> afterSealOrderDetailsResponses = new ArrayList<>();
        // 获取全部供应商
        List<JmSupplierResponse> responses = getSupply();
        //获取全部发货仓
        ResponseResult<List<JmWarehouseResponse>> iJmWarehouseServiceAll = warehouseClient.findAll();
        List<JmWarehouseResponse> jmWarehouseResponses = iJmWarehouseServiceAll.getData();
        AfterSealOrderDetailsResponse afterSealOrderDetailsResponse = new AfterSealOrderDetailsResponse();
        BeanUtils.copyProperties(afterSealOrderGoods, afterSealOrderDetailsResponse);
        afterSealOrderDetailsResponse.setLogisticsOrder(afterSealOrder.getRealLogisticsNo());
        if (afterSealOrderGoods.getSelfFlag().equals(SelfFlagEnum.SELF_FLAG_YES.getCode().byteValue())) {
            jmWarehouseResponses.forEach(
                    jmWarehouseResponse -> {
                        if (afterSealOrderGoods.getWarehouse().equals(jmWarehouseResponse.getWarehouseId().intValue())) {
                            afterSealOrderDetailsResponse.setRealWarehouseName(jmWarehouseResponse.getWarehouseName());
                        }
                    }
            );
        } else if (afterSealOrderGoods.getSelfFlag().equals(SelfFlagEnum.SELF_FLAG_NO.getCode().byteValue())) {
            responses.forEach(
                    jmSupplierResponse -> {
                        if (afterSealOrderGoods.getSupplierId().equals(jmSupplierResponse.getSupplierId().intValue())) {
                            afterSealOrderDetailsResponse.setRealWarehouseName(jmSupplierResponse.getSupplierCompanyName());
                        }
                    }
            );
        }
        afterSealDetailsResponse.setConfirmTime(null);
        if(afterSealOrder.getConfirmTime()!=null && afterSealOrder.getOrderStatus().equals(OrderStatusEnum.COMPLETED.getCode())){
            afterSealDetailsResponse.setConfirmTime(DateUtil.format(afterSealOrder.getConfirmTime(),"yyyy-MM-dd HH:mm:ss"));
        }
        afterSealOrderDetailsResponses.add(afterSealOrderDetailsResponse);
        afterSealDetailsResponse.setOrderGoodsDetailsResponse(afterSealOrderDetailsResponses);
        responseResponseResult.setData(afterSealDetailsResponse);
        return responseResponseResult;
    }

    public TableDataInfo<AfterSealAllResponse> getImportList(AfterSealAllRequest request) {
        TableDataInfo<AfterSealAllResponse> afterSealAllResponseTableDataInfo = new TableDataInfo<>();
        List<AfterSealAllResponse> afterSealAllResponses = new ArrayList<>();
        QueryWrapper<AfterSealOrder> queryWrapper = new QueryWrapper<>();
        if (request.getPhoneType() != null && request.getPhoneType().equals(PhoneTypeEnum.PHONE_TYPE_BUY.getCode())) {
            ResponseResult<Integer> uid = iMemberService.getUidByMobile(request.getPhone());
            if (uid != null) {
                queryWrapper.eq("buyer_id", uid.getData());
            } //按着购买人筛选
        } else if (request.getPhoneType() != null && request.getPhoneType().equals(PhoneTypeEnum.PHONE_TYPE_RECEIVE.getCode())) {
            queryWrapper.eq("receiver_phone", request.getPhone());
        }
        if (request.getOrderNo() != null && request.getOrderNo() != null) {
            queryWrapper.eq("order_no", request.getOrderNo());
        }
        if (request.getStartTime() != null && request.getEndTime() != null) {
            queryWrapper.between("create_time", request.getStartTime(), request.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<AfterSealOrder> afterSealOrderPage = new Page<>(request.getPageNum(), request.getPageSize());
        afterSealOrderPage = afterSealOrderMapper.selectPage(afterSealOrderPage, queryWrapper);
        List<Integer> userId = afterSealOrderPage.getRecords().stream().map(AfterSealOrder::getId).collect(Collectors.toList());
        QueryWrapper<AfterSealOrderGoods> afterSealOrderGoodsQueryWrapper = new QueryWrapper<>();
        if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER_TYPE_ONE.getCode())) {
            afterSealOrderGoodsQueryWrapper.eq("supplier_id", request.getWarehouse());
        } else if (request.getType() != null && request.getType().equals(SupplierTypeEnum.SUPPLIER_TYPE_TWO.getCode())) {
            afterSealOrderGoodsQueryWrapper.eq("warehouse", request.getWarehouse());
        }
        if (request.getGoodsName() != null && request.getGoodsName() != null) {
            afterSealOrderGoodsQueryWrapper.like("goods_name", request.getGoodsName());
        }
        afterSealOrderGoodsQueryWrapper.orderByDesc("create_time");
        afterSealOrderGoodsQueryWrapper.eq("is_import", IsImportEnum.YES.getCode());
        IPage<AfterSealOrderGoods> afterSealOrderGoodsPage = new Page<>(request.getPageNum(), request.getPageSize());
        afterSealOrderGoodsPage = afterSealOrderGoodsMapper.selectPage(afterSealOrderGoodsPage, afterSealOrderGoodsQueryWrapper);
        List<Integer> goodsId = afterSealOrderGoodsPage.getRecords().stream().map(AfterSealOrderGoods::getOrderId).collect(Collectors.toList());
        List<Integer> orderId = goodsId.stream().filter(userId::contains).collect(Collectors.toList());
        if (orderId.size() == 0) {
            return afterSealAllResponseTableDataInfo;
        }
        //获取全部供应商
        List<JmSupplierResponse> responses = getSupply();
        //获取全部发货仓
        ResponseResult<List<JmWarehouseResponse>> iJmWarehouseServiceAll = warehouseClient.findAll();
        List<JmWarehouseResponse> jmWarehouseResponses = iJmWarehouseServiceAll.getData();
        queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", orderId);
        queryWrapper.orderByDesc("create_time");
        List<AfterSealOrder> afterSealOrders = afterSealOrderMapper.selectList(queryWrapper);
        for (AfterSealOrder afterSealOrder : afterSealOrders) {
            AfterSealAllResponse afterSealAllResponse = new AfterSealAllResponse();
            BeanUtils.copyProperties(afterSealOrder, afterSealAllResponse);
            afterSealAllResponse.setAfterSealCreateTime(afterSealOrder.getCreateTime().toString());
            AfterSealUserResponse afterSealUserResponse = new AfterSealUserResponse();
            afterSealUserResponse.setUserName(afterSealOrder.getReceiverName());
            afterSealUserResponse.setAddress(afterSealOrder.getReceiverAddress());
            afterSealUserResponse.setPhone(afterSealOrder.getReceiverPhone());
            afterSealAllResponse.setUser(afterSealUserResponse);
            OrderStatusEnum orderStatusEnum = EnumUtil.getByCode(afterSealOrder.getOrderStatus(), OrderStatusEnum.class);
            afterSealAllResponse.setOrderStatusString(orderStatusEnum.getMessage());
            afterSealAllResponse.setAfterSealCreateTime(DateUtil.getTime(afterSealOrder.getCancelTime()));
            afterSealAllResponses.add(afterSealAllResponse);
        }
        for (AfterSealAllResponse afterSealAllResponse : afterSealAllResponses) {
            List<AfterSealGoodsResponse> afterSealGoodsResponses = new ArrayList<>();
            for (AfterSealOrderGoods record : afterSealOrderGoodsPage.getRecords()) {
                if (afterSealAllResponse.getId() == record.getOrderId()) {
                    AfterSealGoodsResponse afterSealGoodsResponse = new AfterSealGoodsResponse();
                    BeanUtils.copyProperties(record, afterSealGoodsResponse);
                    if (record.getSelfFlag().equals(SupplierTypeEnum.SUPPLIER_TYPE_ONE.getCode().byteValue())) {
                        for (JmSupplierResponse response : responses) {
                            if (record.getSupplierId().equals(response.getSupplierId().intValue())) {
                                afterSealGoodsResponse.setWarehouse(response.getSupplierCompanyName());
                                afterSealGoodsResponse.setWarehouseId(record.getSupplierId());
                            }
                        }
                    } else if (record.getSelfFlag().equals(SupplierTypeEnum.SUPPLIER_TYPE_TWO.getCode().byteValue())) {
                        for (JmWarehouseResponse jmWarehouseRespons : jmWarehouseResponses) {
                            if (record.getWarehouse().equals(jmWarehouseRespons.getWarehouseId().intValue())) {
                                afterSealGoodsResponse.setWarehouse(jmWarehouseRespons.getWarehouseName());
                                afterSealGoodsResponse.setWarehouseId(record.getWarehouse());
                            }
                        }
                    }
                    afterSealGoodsResponses.add(afterSealGoodsResponse);
                }
            }
            afterSealAllResponse.setGoodsList(afterSealGoodsResponses);
        }
        afterSealAllResponseTableDataInfo.setTotal(afterSealOrderGoodsPage.getTotal());
        afterSealAllResponseTableDataInfo.setRows(afterSealAllResponses);
        return afterSealAllResponseTableDataInfo;
    }

    /**
     * 获取所有供应商
     *
     * @return
     */
    public List<JmSupplierResponse> getSupply() {
        ResponseResult<List<JmSupplierResponse>> responseResult = supplyClient.list();
        List jmSupplierResponses = responseResult.getData();
        List<JmSupplierResponse> result = new ArrayList<>();
        for (Object object : jmSupplierResponses) {
            JmSupplierResponse jmSupplierResponse = JSON.parseObject(JSON.toJSONString(object), JmSupplierResponse.class);
            result.add(jmSupplierResponse);
        }
        return result;
    }
}
